<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
           function getAjax(url,data){
        return new Promise((resolve,reject) => {
            // 创建对象
        let xhr=new XMLHttpRequest();
        //注册状态改变的函数
        xhr.onreadystatechange=function(){
            if(xhr.readyState === 4){
                //判断是否成功
                if(xhr.status>=200 && xhr.status<300){
                    //获取响应数据
                    resolve(xhr.responseText);
                }else{
                    reject(xhr.responseText);
                }
            }
        }
        //打开链接
        xhr.open('GET',url);
        //发送请求
        xhr.send();        
    } )
}

function postAjax(url,data){
    return new Promise((resolve,reject)=>{
        //创建对象
        let xhr = new XMLHttpRequest;
        //注册状态改变的回调函数
        xhr.onreadystatechange=function(){
            if(xhr.readyState === 4){
                //判断是否成功
                if(xhr.status>=200 && xhr.status<300){
                    // 获取响应数据
                    resolve(xhr.responseText)
                }else{
                    reject(xhr.responseText)
                }
            }
        }
        //打开链接
        xhr.open('POST',url);
        //表单格式:class=五期&name=张三
        xhr.setRequestHeader('Content-type',"application/x-www-form-unlencoded")
        // 发送请求 post的请求需要添加参数
        xhr.send(data)

        //打开链接
        // xhr.open("GET",url);
        // //json格式
        // xhr.setRequestHeader('Content-type',"application/json");
        // //发送请求
        // xhr.send(JSON.stringify(data));
    })
}
            //  传入数据格式
//     let pro =getAjax(' http://localhost:3008/api/student/getStudent',{
//     "clazz": "huohau20",
//    "name": "111111111111",
//    "gender": "male",
//    "age": "18",
//    "hobby": "read",
//    "tel": "18537289347",
//    "address": "luohe",
//    "remark": "单身程序员",
//    "id": "1726055238827",
//    "date": "2024/9/11"
//     })
             // json对象传数据格式
    let pro = postAjax('http://localhost:3008/api/student/addStudent',{
    "clazz": "huohau20",
   "name": "111111111111",
   "gender": "male",
   "age": "18",
   "hobby": "read",
   "tel": "18537289347",
   "address": "luohe",
   "remark": "单身程序员",
   "id": "1726055238827",
   "date": "2024/9/11"
    })
            // 表单格式（字符串）
        // let pro = postAjax('http://localhost:3008/api/student/addStudent',
        // 'class=五期&name=张三&gender=男&age=20&tel=1001&address=郑州')
        console.log(pro);
        pro.then((result)=>{
            console.log(result);
        }).catch((err)=>{
            console.log(err);
        })
        

    </script>

</body>
</html>